﻿--[[
  Aprs4G by BG2LBF - Watchdog (original logic kept)
  NOTE: This file restores the original behavior:
    - air780eg-yed: use external AIR153C watchdog on GPIO 28
    - other boards (e.g. air780eg_a12): use internal wdt (9s timeout, feed every 4s)
]]

local function use_ext_wdt()
    return DEV_TYPE == "air780eg-yed"
end

if use_ext_wdt() then
    require "air153C_wtd"      -- external watchdog helper
else
    -- internal wdt may be absent on some bases; warn and skip init
    if not wdt then
        log.warn("wdt", "wdt lib not found, skip init")
    end
end

sys.taskInit(function()
    if use_ext_wdt() then
        -- external WDT (YED board)
        local WDT_PIN = 28
        air153C_wtd.init(WDT_PIN)
        air153C_wtd.feed_dog(WDT_PIN)      -- feed once at power on
        sys.wait(3000)

        log.info("WTD", "external dog start")
        while true do
            air153C_wtd.feed_dog(WDT_PIN)
            log.info("main", "feed dog (ext)")
            sys.wait(150000)
        end

    else
        -- internal WDT (air780eg / air780eg_a12 / etc.)
        if wdt then
            log.info("main", "添加看门狗防止程序卡死 (internal)")
            wdt.init(9000)                  -- 9s timeout (original value)
            sys.timerLoopStart(wdt.feed, 4000)  -- feed every 4s (original)
        else
            -- no wdt lib, just warn
            log.warn("wdt", "this app need wdt lib, but not present")
        end
    end
end)
